package SHClient;

import ocsf.client.*;
import common.*;

import SHGui.*;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;

import javax.swing.JFrame;

//import com.mysql.jdbc.Blob;

import SHClient.ListVar;
import SHClient.Main;

/**
 * This class overrides some of the methods defined in the abstract
 * superclass in order to give more functionality to the client.
 *
 * @author Dr Timothy C. Lethbridge
 * @author Dr Robert Lagani&egrave;
 * @author Fran&ccedil;ois B&eacute;langer
 * @version July 2000
 */
public class ChatClient extends AbstractClient
{
  //Instance variables **********************************************
  
  /**
   * The interface type variable.  It allows the implementation of 
   * the display method in the client.
   */
  ChatIF clientUI;
  Login_Ctrl lc = new Login_Ctrl(1);
 
  static public LinkedList<ListVar> panels_order = new LinkedList<ListVar>();
  public String globalId=" ";

  
  //Constructors ****************************************************
  
  /**
   * Constructs an instance of the chat client.
   *
   * @param host The server to connect to.
   * @param port The port number to connect on.
   * @param clientUI The interface type variable.
   */
  
  public ChatClient(String host, int port, ChatIF clientUI) 
    throws IOException 
  {
    super(host, port); //Call the superclass constructor
    this.clientUI = clientUI;
    openConnection();

  }
 
  
 

  //Instance methods ************************************************
    
  /**
   * This method handles all data that comes in from the server.
   *
   * @param msg The message from the server.
 * @throws IOException 
   */
  public void handleMessageFromServer(Object msg) 
  {		Object[] s= (Object[]) msg;
	  System.out.println((String)s[0]+"chatclient");
	  
	  switch((String)s[0]){
	  case "Login": Main.lc1.cc.globalId=(String)s[1];
	  				System.out.println(Main.lc1.cc.globalId +"in cccccc");
	  				try {
	  					Main.lc1.handle_result(msg);
	  					} catch (IOException e) {e.printStackTrace();}
	 	  				break;
	  case "area": System.out.println("in handleMessageFromServer area");
		try {
			Main.lc1.handle_result(msg);
			} catch (IOException e) {e.printStackTrace();}
				break;
	  case "Lamp_GUi": System.out.println("in handleMessageFromServer Lamp_GUi");
		try {
			Main.pc.handle(msg);
			} catch (IOException e) {e.printStackTrace();}
				break;
	  case "AirCon_GUi": System.out.println("in handleMessageFromServer AirCon_GUi");
		try {
			Main.pc.handle(msg);
			} catch (IOException e) {e.printStackTrace();}
				break;
     case "Oven_GUI": System.out.println("in handleMessageFromServer Oven_GUI");
	try {
		Main.pc.handle(msg);
		} catch (IOException e) {e.printStackTrace();}
			break;
     case "Freezer_GUI": System.out.println("in handleMessageFromServer Oven_GUI");
 	try {
 		Main.pc.handle(msg);
 		} catch (IOException e) {e.printStackTrace();}
 			break;
     case "Shutters_GUI": System.out.println("in handleMessageFromServer Shutters_GUI");
  	try {
  		Main.pc.handle(msg);
  		} catch (IOException e) {e.printStackTrace();}
  			break;
     case "Laundry_GUI": System.out.println("in handleMessageFromServer Laundry_GUI");
   	try {
   		Main.pc.handle(msg);
   		} catch (IOException e) {e.printStackTrace();}
   			break;
     case "Draier_GUI": System.out.println("in handleMessageFromServer Draier_GUI");
    	try {
    		Main.pc.handle(msg);
    		} catch (IOException e) {e.printStackTrace();}
    			break;
     case "Socket_GUI": System.out.println("in handleMessageFromServer Socket_GUI");
 	try {
 		Main.pc.handle(msg);
 		} catch (IOException e) {e.printStackTrace();}
 			break;
     case "TV_GUI": System.out.println("in handleMessageFromServer TV_GUI");
  	try {
  		Main.pc.handle(msg);
  		} catch (IOException e) {e.printStackTrace();}
  			break;
	  case "combination": System.out.println("in handleMessageFromServer combination");
		try {
			Main.lc1.handle_result(msg);
			} catch (IOException e) {e.printStackTrace();}
				break;
	  case "optimization": System.out.println("in handleMessageFromServer optimization");
			try {
				Main.lc1.handle_result(msg);
				} catch (IOException e) {e.printStackTrace();}
					break;
	    }
     
  }
  

  
  
  

  /**
   * This method handles all data coming from the UI            
   *
   * @param message The message from the UI.    
   */
  public void handleMessageFromClientUI(Object message)
  {
	  System.out.println("handleMessageFromClientUI");
	  String[] str=(String[])(message);
	  System.out.println(str[0]+"uuuuuuuuoooooooooouuuuuuuuo");
	
try
    {
         
    	 sendToServer(message);
    	 System.out.println("ghghghghgh");
    }
    catch(IOException e)
    {
      clientUI.display
        ("Could not send message to server.  Terminating client.");
      quit();
    }
  }
  
  /**
   * This method terminates the client.
   */
  public void quit()
  {
    try
    {
      closeConnection();
    }
    catch(IOException e) {}
    System.exit(0);
  }
}
//End of ChatClient class